<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="报价名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入报价名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="number">
              <a-input v-model="model.number" placeholder="请输入编号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="客户ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customerId">
              <j-search-select-tag v-model="model.customerId" dict="biz_customer,name,id" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="联系人IDS" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contactsIds">
              <j-multi-select-tag type="list_multi" v-model="model.contactsIds" dictCode="biz_contacts,name,id"
                                  placeholder="请选择联系人IDS" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="机会ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="opportunityId">
              <j-search-select-tag v-model="model.opportunityId" dict="biz_sales_opportuninty,name,id" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="报价ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="quotationId">
              <j-search-select-tag v-model="model.quotationId" dict="biz_quotation,name,id" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="签订人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="signedBy">
              <a-input v-model="model.signedBy" placeholder="请输入签订人"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="签订时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="signedTime">
              <j-date placeholder="请选择签订时间" v-model="model.signedTime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="到期时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endTime">
              <j-date placeholder="请选择到期时间" v-model="model.endTime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="合同状态(1.签约 2.执行中 3.完毕 4.终止)" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="state">
              <j-dict-select-tag type="list" v-model="model.state" dictCode="contract_order_state"
                                 placeholder="请选择合同状态(1.签约 2.执行中 3.完毕 4.终止)" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="负责人IDS" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="directorIds">
              <j-select-user-by-dep v-model="model.directorIds" :multi="true" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="协同人IDS" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="collaboratorIds">
              <j-select-user-by-dep v-model="model.collaboratorIds" :multi="true" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="产品IDS" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productIds">
              <j-search-select-tag v-model="model.productIds" dict="biz_product,name,id" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="产品合计" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="totalProducts">
              <a-input-number v-model="model.totalProducts" placeholder="请输入产品合计" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="折扣率" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="discountRate">
              <a-input v-model="model.discountRate" placeholder="请输入折扣率"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="其他费用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="otherExpenses">
              <a-input-number v-model="model.otherExpenses" placeholder="请输入其他费用" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="优惠价钱" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="preferentialAmount">
              <a-input-number v-model="model.preferentialAmount" placeholder="请输入优惠价钱" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="合同价钱" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractAmount">
              <a-input-number v-model="model.contractAmount" placeholder="请输入合同价钱" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="回款单ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paymentReceiptId">
              <a-input-number v-model="model.paymentReceiptId" placeholder="请输入回款单ID" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="应收款IDS" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="receivablesIds">
              <j-dict-select-tag type="list" v-model="model.receivablesIds" dictCode="biz_receivables,number,id"
                                 placeholder="请选择应收款IDS" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="合同类型(1.直销合同 2.代理合同 3.服务合同 4.快销合同)" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="contractType">
              <j-dict-select-tag type="list" v-model="model.contractType" dictCode="contract_order_type"
                                 placeholder="请选择合同类型(1.直销合同 2.代理合同 3.服务合同 4.快销合同)" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="付款方式(1.网银转账 2.现金 3.支票  4.电汇 5.承兑汇票)" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="payType">
              <j-dict-select-tag type="list" v-model="model.payType" dictCode="contract_pay_type"
                                 placeholder="请选择付款方式(1.网银转账 2.现金 3.支票  4.电汇 5.承兑汇票)" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="发货状态(1.缺货 2.待发货 3.已发货)" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="deliverType">
              <j-dict-select-tag type="list" v-model="model.deliverType" dictCode="contract_deliver_state"
                                 placeholder="请选择发货状态(1.缺货 2.待发货 3.已发货)" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="物流公司(1.顺丰 2.圆通 3.天天)" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="logisticsCompany">
              <j-dict-select-tag type="list" v-model="model.logisticsCompany" dictCode="contract_logistics_company"
                                 placeholder="请选择物流公司(1.顺丰 2.圆通 3.天天)" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="物流编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="logisticsNumber">
              <a-input v-model="model.logisticsNumber" placeholder="请输入物流编号"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'ContractOrderForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        name: [
          { required: true, message: '请输入报价名称!' }
        ],
        number: [
          { required: true, message: '请输入编号!' }
        ],
        customerId: [
          { required: true, message: '请输入客户ID!' }
        ],
        contactsIds: [
          { required: true, message: '请输入联系人IDS!' }
        ],
        opportunityId: [
          { required: true, message: '请输入机会ID!' }
        ],
        quotationId: [
          { required: true, message: '请输入报价ID!' }
        ],
        signedTime: [
          { required: true, message: '请输入签订时间!' }
        ],
        directorIds: [
          { required: true, message: '请输入负责人IDS!' }
        ],
        collaboratorIds: [
          { required: true, message: '请输入协同人IDS!' }
        ],
        productIds: [
          { required: true, message: '请输入产品IDS!' }
        ],
        paymentReceiptId: [
          { required: true, message: '请输入回款单ID!' }
        ],
        receivablesIds: [
          { required: true, message: '请输入应收款IDS!' }
        ],
        contractType: [
          { required: true, message: '请输入合同类型(1.直销合同 2.代理合同 3.服务合同 4.快销合同)!' }
        ],
        payType: [
          { required: true, message: '请输入付款方式(1.网银转账 2.现金 3.支票  4.电汇 5.承兑汇票)!' }
        ]
      },
      url: {
        add: '/contractorder/contractOrder/add',
        edit: '/contractorder/contractOrder/edit',
        queryById: '/contractorder/contractOrder/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    }
  }
}
</script>